class Admin::ProductPhotosController < Admin::AdminController
  before_filter :load_product, :except => [:destroy]

  filter_access_to :all

  def index
    
  end

  def new
    @product_photo = Photo.new

    respond_to do |format|
      format.html
      format.js {render :action => "new", :layout => false}
    end
  end

  def create
    @product_photo = Photo.new(params[:photo])
    @product.photos << @product_photo

    if @product.save
      redirect_to edit_admin_product_product_photo_path(:product_id => @product.id, :id => @product_photo.id), :notice => "Fotka byla úspěšně uložena."
    else
      render :action => :new
    end
  end

  def edit
    @product_photo = Photo.find(params[:id])

    respond_to do |format|
      format.html
      format.js {render :action => "edit", :layout => false}
    end
  end

  def update
    @product_photo = Photo.find(params[:id])

    if @product_photo.update_attributes(params[:photo])
      redirect_to admin_product_product_photos_path(params[:product_id]), :notice => "Fotka byla úspěšně upravena"
    else
      render :action => :edit
    end
  end

  def destroy
    @photo = Photo.find(params[:id])

    if @photo.destroy
      flash[:notice] = "Fotku se podařilo úspěšně smazat."
    else
      flash[:error] = "Fotku se nepodařilo smazat."
    end

    redirect_to admin_product_product_photos_path(params[:product_id])
  end

  def move
    case params[:method]
      when "move_lower"
        @photo.move_lower
        flash[:notice] = "Fotka byla úspěšně posunuta vpravo."
      when "move_higher"
        @photo.move_higher
        flash[:notice] = "Fotka byla úspěšně posunuta vlevo."
      when "move_to_top"
        @photo.move_to_top
        flash[:notice] = "Fotka byla označena jako hlavní."
      else
        flash[:error] = "Fotku se nepodařilo posunout"
      end

    redirect_to admin_product_photos_path(:product_id => params[:product_id])
  end
  
  private
  def load_product
    @product = Product.find(params[:product_id] || params[:product])
  end
end
